<?php
namespace app\index\controller;
use app\index\controller\BaseController;
use app\common\model\UserModel as User;
use think\Request;
/**
 * 
 */
class UserController extends BaseController
{
	
	function __construct()
	{
		parent::__construct();
	}

	// 会员中心
	public function indexAction(){
		return $this->fetch();
	}

	public function regAction(User $user,Request $request){ 
		if ($request->isPost()) { 
			$res=$user->reg($request->param());
			if ($res) {
				$this->success('注册成功');
			}else{
				$this->error($user->getError());
			}
		}
		return $this->fetch();
	}

	public function loginAction(User $user,Request $request){
		if ($request->isPost()) {
			$user_id=$user->login($request->param()); 
			if ($user_id) { 
				$this->success('登录成功');
			}else{
				$this->error($user->getError());
			}
		}
		return $this->fetch();
	}

	// 编辑信息
	public function editAction(User $user,Request $request){ 
		if ($request->isPost()) {
			$res=$user->edit($request->param());
			if ($res) {
				$this->success('编辑成功');
			}else{
				$this->error($user->getError());
			}
		}
		$this->assign([
			'r'=>$user->find(getCookieUserid()),
		]);
		return $this->fetch();
	}

	// 修改密码
	public function editPwdAction(User $user,Request $request){
		if ($request->isPost()) {
			$res=$user->editPwd(getCookieUserid(),$request->param('password'));
			if ($res) {
				$this->success('修改成功');
			}else{
				$this->error($user->getError());
			}
		}
		return $this->fetch();
	}

	public function logoutAction(User $user){
		$res=$user->logout();
		if ($res) {
			$this->success('退出成功');
		}else{
			$this->error($user->getError());
		}
	}
}